﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ProgNetComponents.Data.SqlCe;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace BuildHelper.Admin
{
    public static class AdminTools
    {
        private static string Separator = "^GO;?\\s*$";
        
        public static string ExecuteRemote(string queries)
        {
            List<string> l = new List<string>();
            string str = queries.ReplaceDoubleEOL();
            l.AddRange(str.XSplit(Separator, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase));
            string result = "";


            int i = 0;
            string ss;
            using (SqlCeDataManager Manager = new SqlCeDataManager())
            {
                Manager.AutoCommit = true;
                foreach (string s in l)
                {
                    result += i+"|";
                    ss = s.Trim();
                    try
                    {
                        if (ss.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
                        {
                            string tableName = ss.GetFirstMatch("FROM\\s*(\\w*)");
                            if (tableName.IsNull())
                                tableName = "Table1";
                            DataTable dt = new DataTable(tableName);
                            if (Manager.FillTable(dt, ss))
                            {
                                result += "DT" + "|";
                                DataSet ds = new DataSet("DataSet");
                                ds.Tables.Add(dt);
                                result += ds.WriteXmlString();
                            }
                            else
                            {
                                if (Manager.LastException != null)
                                    result += "EX|" + Manager.LastException.Message;
                                else
                                    result += "EX|Exception!";
                            }
                        }
                        else
                        {
                            int k = Manager.ExecuteNonQuery(ss);
                            result += "CT|" + k;
                        }
                    }
                    catch (Exception ex)
                    {
                        result += "EX|" + ex.Message;
                    }
                }
            }
            return result;
        }

        public static string WriteXmlString(this DataTable dt)
        {
            string result = string.Empty;
            using (System.IO.StringWriter sw = new System.IO.StringWriter())
            {
                dt.WriteXml(sw);
                result = sw.ToString();
            }
            return result;
        }
        public static void ReadXmlString(this DataTable dt, string xml)
        {
            using (System.IO.StringReader sw = new System.IO.StringReader(xml))
            {
                dt.ReadXml(sw);
            }
        }
        public static string WriteXmlString(this DataSet ds, XmlWriteMode mode = XmlWriteMode.WriteSchema)
        {
            string result = string.Empty;
            using (System.IO.StringWriter sw = new System.IO.StringWriter())
            {
                ds.WriteXml(sw, mode);
                result = sw.ToString();
            }
            return result;
        }
        public static void ReadXmlString(this DataSet ds, string xml, XmlReadMode mode = XmlReadMode.InferSchema)
        {
            using (System.IO.StringReader sw = new System.IO.StringReader(xml))
            {
                ds.ReadXml(sw, mode);
            }
        }
        public static void ShowSnapshot(string sender, byte[] bytes)
        {
            try
            {
                if (bytes != null)
                {
                    using (Stream s = new MemoryStream(bytes))
                    {
                        sender = sender.GetFirstMatch("(\\w*)");
                        string f = sender;
                        if (f.IsNullOrEmpty() || f.IndexOfAny(Path.GetInvalidPathChars()) > -1)
                            f = "snapshot";
                        string tmpFile = Application.StartupPath + "\\" + f + ".jpeg";
                        using (Image bmp = Image.FromStream(s))
                        {
                            bmp.Save(tmpFile);

                            if (File.Exists(tmpFile))
                            {
                                if (FormMain.Instance.ShowDxYNQuestionDialog("Otrzymano printscreen od " + sender + ". Chcesz go wyświetlić?") == DialogResult.Yes)
                                {
                                    Process.Start(tmpFile);
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                FormMain.Instance.ShowDxInfoDialog("Wystąpił problem z odebraniem printscreena od " + sender + ".");
            }
        }
    }
}
